package com.huike.clues.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.SysDictTypeAddDTO;
import com.huike.clues.domain.dto.SysDictTypeQueryDTO;
import com.huike.clues.domain.result.PageResult;
import com.huike.clues.mapper.SysDictTypeMapper;

import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper,SysDictType>implements ISysDictTypeService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    /**
     * 分页查询数据字典类型信息
     *
     * @param dto 前端传过来的数据
     * @return 封装后的数据
     */
    @Override
    public List<SysDictType> selectList(SysDictTypeQueryDTO dto) {
//        IPage<SysDictType> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getDictName()!=null&&dto.getDictName()!=""){
            queryWrapper.like(SysDictType::getDictName, dto.getDictName());
        }
        if (dto.getDictType()!=null&&dto.getDictType()!=""){
            queryWrapper.like(SysDictType::getDictType, dto.getDictType());
        }
        if (dto.getStatus()!=null&&dto.getStatus()!=""){
            queryWrapper.eq(SysDictType::getStatus, dto.getStatus());
        }
        if (dto.getBeginTime()!=null&&dto.getBeginTime()!=""&&dto.getEndTime()!=null&&dto.getEndTime()!=""){
            queryWrapper.between(SysDictType::getCreateTime,dto.getBeginTime(),dto.getEndTime());
        }
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectList(queryWrapper);
//        PageResult<SysDictType> pageResult = new PageResult<>();
//        pageResult.setTotal(selectPage.getTotal());
//        pageResult.setRows(selectPage.getRecords());
//        System.out.println(selectPage.getTotal());
//        pageResult.setTotal(selectPage.getTotal());
        return sysDictTypes;
    }

    /**
     * 新增字典类型
     *
     * @param dto 前端传过来的数据
     */
    @Override
    public void insert(SysDictTypeAddDTO dto) {
        SysDictType dictType = BeanUtil.toBean(dto, SysDictType.class);
        sysDictTypeMapper.insert(dictType);
    }
}
